package cn.iocoder.yudao.module.myerp.service.SPUproduct;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.myerp.controller.admin.SPUproduct.vo.*;
import cn.iocoder.yudao.module.myerp.dal.dataobject.SPUproduct.ErpProductSpuDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.myerp.dal.mysql.SPUproduct.ErpProductSpuMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.myerp.enums.ErrorCodeConstants.*;

/**
 * ERP SPU级产品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ErpProductSpuServiceImpl implements ErpProductSpuService {

    @Resource
    private ErpProductSpuMapper erpProductSpuMapper;

    @Override
    public Long createErpProductSpu(ErpProductSpuSaveReqVO createReqVO) {
        // 插入
        ErpProductSpuDO erpProductSpu = BeanUtils.toBean(createReqVO, ErpProductSpuDO.class);
        erpProductSpuMapper.insert(erpProductSpu);
        // 返回
        return erpProductSpu.getId();
    }

    @Override
    public void updateErpProductSpu(ErpProductSpuSaveReqVO updateReqVO) {
        // 校验存在
        validateErpProductSpuExists(updateReqVO.getId());
        // 更新
        ErpProductSpuDO updateObj = BeanUtils.toBean(updateReqVO, ErpProductSpuDO.class);
        erpProductSpuMapper.updateById(updateObj);
    }

    @Override
    public void deleteErpProductSpu(Long id) {
        // 校验存在
        validateErpProductSpuExists(id);
        // 删除
        erpProductSpuMapper.deleteById(id);
    }

    private void validateErpProductSpuExists(Long id) {
        if (erpProductSpuMapper.selectById(id) == null) {
            throw exception(ERP_PRODUCT_SPU_NOT_EXISTS);
        }
    }

    @Override
    public ErpProductSpuDO getErpProductSpu(Long id) {
        return erpProductSpuMapper.selectById(id);
    }

    @Override
    public PageResult<ErpProductSpuDO> getErpProductSpuPage(ErpProductSpuPageReqVO pageReqVO) {
        return erpProductSpuMapper.selectPage(pageReqVO);
    }

}